<?php
namespace Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\Platform\Mysql;
use Zend\Db\Adapter\Platform\Sql92;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;

class RoleTable extends AbstractTable
{

    /**
     * @param $id
     * @return Role
     */
    public function getRole($id){
       return $this->getById($id);
    }

    /**
     * @param $name
     * @return Role
     */
    public function getRoleByName($name)
    {
       return $this->getByColumn('name', $name)->current();
    }

    /**
     * @param $code
     * @return Role
     */
    public function getRoleByCode($code)
    {
        return $this->getByColumn('code', $code)->current();
    }

    public function searchAllRole($isPaging, $offset, $rowPerPage, $orderArr = array('name'), $searchOption){

        $select = $this->getDbSqlSelect();

        if($searchOption['name']){
            $select->where(array('name like "%'.$searchOption['name'].'%"'));
        }

        if($isPaging){
            $select->offset($offset)->limit($rowPerPage);
        }

        if($orderArr){
            $select->order($orderArr);
        }

        $rows = $this->tableGateway->selectWith($select);
        $result['rows'] = $rows;

        $total = $this->getCount($select);

        $result['total'] = $total;

        return $result;
    }

    public function getRolesContainPrivilege($privilegeId)
    {
        $select = $this->getDbSqlSelect();
        $select->join(array('rp'=>'role_privilege'),
            'rp.role_id = role.id', array())
            ->where(array('rp.privilege_id'=>$privilegeId));
        return $this->getTableGateway()->selectWith($select);
    }


}